//
//  ECMineCourseSubViewController.m
//  Edu_iOS2018
//
//  Created by apple on 2018/1/6.
//  Copyright © 2018年 yanyan. All rights reserved.
//

#import "ECMineCourseSubViewController.h"
#import "ECMineCourseSubCell.h"
#import "ECMineCourseSubModel.h"

#import "CYDataHelper.h"

#import "ECUIVC_CourseDetail.h"
#import "CourseModel.h"
#import "UIImage+RenderedImage.h"

@interface ECMineCourseSubViewController ()<UITableViewDelegate,UITableViewDataSource,DZNEmptyDataSetSource,DZNEmptyDataSetDelegate>

@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray *dataArray;
///总个数
@property (nonatomic, assign) int allPageNum;

@end

@implementation ECMineCourseSubViewController

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController.navigationBar lt_setBackgroundColor:[UIColor whiteColor]];
    self.navigationController.navigationBar.shadowImage = [UIImage imageWithColor: [[UIColor darkGrayColor] colorWithAlphaComponent:1] renderSize:CGSizeMake(kScreenWidth, 0.20)];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"课程评价";
    [self.view addSubview:self.tableView];
    
    [self getDataWithURL];
}

/*  加载数据  */
-(void)getDataWithURL{
    DLog(@"%@",[[UserInfoManager shareUser] token]);
    [HWHttpTool requestWithMethod:@"GET"
                              url:URL_GET_Reviews
                           params:@{@"start":@(self.dataArray.count)}
                          headers:@{@"X-AUTH-TOKEN":[[UserInfoManager shareUser] token]}
                          success:^(id json) {
                              [self.tableView.mj_footer endRefreshing];//结束刷新
                              self.allPageNum = [json[@"data"][@"total"] intValue];//总个数
                              
                              NSArray *array = json[@"data"][@"resources"];
                              [self.dataArray addObjectsFromArray: [ECMineCourseSubModel mj_objectArrayWithKeyValuesArray:array]];
                              if (self.dataArray.count >= self.allPageNum) {
                                  [self.tableView.mj_footer endRefreshingWithNoMoreData];
                                  self.tableView.mj_footer.hidden = YES;

                              } else {
                                  [self.tableView.mj_footer resetNoMoreData];
                                  self.tableView.mj_footer.hidden = NO;
                              }
                              [self.tableView reloadData];
                          }
                          failure:^(NSError *error) {
                              [self.tableView.mj_footer endRefreshing];
                              NSString *str = [ECData_Json requestErrorTipWith:error];
                              if (!([str isEqualToString:@"101"] || [str isEqualToString:@"401"])) {
                                  [MBProgressHUD showTipMessageInWindow:kAlertTipOfInternetFailure];
                              }
                              DLog(@"%@",str);
                          }];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSString *indetify = @"indetify";
    ECMineCourseSubCell *cell = [tableView dequeueReusableCellWithIdentifier:indetify];
    if (!cell) {
        cell = [[ECMineCourseSubCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indetify];
    }
    [cell setContentMode:self.dataArray[indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    CourseModel *courseModel = [[CourseModel alloc]init];
    ECMineCourseSubModel *model = self.dataArray[indexPath.row];
    courseModel.modelId = model.course.ID;
    ECUIVC_CourseDetail *vc = [[ECUIVC_CourseDetail alloc]init];
    vc.object = courseModel;
    [self.navigationController pushViewController:vc animated:YES];
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    ECMineCourseSubModel *model = self.dataArray[indexPath.row];
    CGFloat Height = [self getHeightWithStr:model.content];
    return kProportion_Height(250)+ Height;
}

/*  获得文字高度  */
-(CGFloat)getHeightWithStr:(NSString *)str{
    
    if ([CYDataHelper isNull:str]) {
        str = @"暂无";
    }
    CGRect rect= [str boundingRectWithSize:CGSizeMake(kProportion_Width(1080-190-45), MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:14*kPercentage]} context:nil];
    return rect.size.height;
}

- (UIImage *)imageForEmptyDataSet:(UIScrollView *)scrollView{
    return [UIImage imageNamed:@"Mine_emptyImage_SystemNoti"];
}

- (NSAttributedString *)titleForEmptyDataSet:(UIScrollView *)scrollView{
    NSString *text = @"暂无任何评价";
    NSDictionary *attributes = @{NSFontAttributeName: [UIFont boldSystemFontOfSize:16.0f],
                                 NSForegroundColorAttributeName: [UIColor colorWithHexString:@"#b3b3b3"]};
    return [[NSAttributedString alloc] initWithString:text attributes:attributes];
}

/*  懒加载tableview  */
-(UITableView *)tableView{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth, kScreenHeight-kNavHeight-kSafeAreaBottomHeight) style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor = [UIColor whiteColor];
        
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
                [self getDataWithURL];
        }];
    }
    return _tableView;
}

/*  懒加载数据  */
-(NSArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc]init];
    }

    
    return _dataArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
